package com.dudu.log.smsLog.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dudu.bean.UserBean;
import com.dudu.log.smsLog.service.ISmsLogService;
import com.dudu.log.smsLog.service.impl.SmsLogServiceImpl;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.HttpRequest;

import core.CoreService;

public class SmsLogAction extends CoreService implements ISmsLogAction {

    // 当前登陆用户
    private final String KEY_CURRENT_USER = "key_current_user";
    private ISmsLogService smsLogService = new SmsLogServiceImpl();

    /**
     * 查询白名单，按分页条件查询
     * 
     * @return
     */
    public String queryAll() {
	log.info("SmsIpController.queryAll");
	JSONObject object = new JSONObject();
	JSONArray jsonArray = new JSONArray();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	if (currentUser != null) {
	    // 获取页面分页用信息
	    String pageSql = this.getPageSql();
	    // 获取查询参数信息
	    String paramSql = this.convertParams();
	    log.info("SmsIpController.queryAll:" + paramSql);
	    // 总数量
	    int totalAcount = this.smsLogService.getTotalAcount(paramSql);
	    List<JSONObject> list = this.smsLogService.queryAll(paramSql, pageSql);
	    jsonArray = JSONArray.fromObject(list);

	    JSONObject dataobject = new JSONObject();
	    object.put("flag", "000000");
	    object.put("desc", "success");
	    dataobject.put("totalItemNum", totalAcount);
	    dataobject.put("items", jsonArray);
	    object.put("data", dataobject);
	} else {
	    object.put("flag", "100000");
	}
	return returnData(object.toString());
    }

    /**
     * 重发
     * 
     * @return
     */
    public String smsRepeat() {
	JSONObject object = new JSONObject();
	// 获取当前登录用户信息
	UserBean currentUser = (UserBean) this.getSession().getAttribute(KEY_CURRENT_USER);
	if (currentUser != null) {
	    try {
		String url = DictionaryUtils.getValueByTypeAndKey("url", "localhost");
		String cust_account = this.getRequest().getParameter("cust_account");
		String user_identifer = this.getRequest().getParameter("user_identifer");
		String msg_id = this.getRequest().getParameter("msg_id");
		String content = this.getRequest().getParameter("content");
		content=new String(content.getBytes("ISO8859-1"), "UTF-8");
		String id = this.getRequest().getParameter("id");
		StringBuffer sb = new StringBuffer();
		sb.append("cust_account=" + cust_account);
		sb.append("&ext_terminalCode=" + user_identifer);
		sb.append("&msg_id=" + msg_id);
		sb.append("&content=" + content);
		sb.append("&voiceid=" + id);
		String data = HttpRequest.post(url + "/SMS/smsRepeat.do", sb);
		JSONObject response = JSONObject.fromObject(data);
		String result = response.optString("result");
		if (result.equals("0")) {
		    object.put("flag", "000000");
		    object.put("desc", "共重发"+response.optString("length")+"条数据");
		} else {
		    object.put("flag", "100000");
		    
		}
	    } catch (Exception e) {
		e.printStackTrace();
		object.put("flag", "100000");
	    }

	} else {
	    object.put("flag", "100000");
	}
	return returnData(object.toString());
    }

}
